<template>
  <customContainer ref="customContainerRef" :config="config" />
  <setForm ref="setFormRef" @changeList="handleQuery" />
</template>
<script lang="ts" setup>
import {
  Delete,
  Refresh,
  Plus,
  Download,
  Edit,
  Document,
  Calendar } from '@element-plus/icons-vue'
import { ref, getCurrentInstance, defineAsyncComponent, computed } from 'vue'
import { ElMessage, ElMessageBox, ElButton } from 'element-plus'
import { DictDirection } from '@/store/modules/dicts'
import {
  listClient,
  getClient,
  delClient,
  addClient,
  updateClient,
} from '@/api/system/client'

const setForm = defineAsyncComponent(() => import('./components/setForm.vue'))
let // 查询参数
  queryParams = ref({
    pageNum: 1,
    pageSize: 10,
    clientId: undefined,
  }),
  // 选中数组
  ids = [],
  // 非单个禁用
  single = ref(true),
  // 非多个禁用
  multiple = ref(true),
  //
  customContainerRef = ref(null),
  // 新增终端
  setFormRef = ref(null)
// 查询
const handleQuery = () => {
  queryParams.value.pageNum = 1
  customContainerRef.value.getList()
}
// 多选框选中数据
const handleSelectionChange = (rows: { [key: string]: any }[]) => {
  ids = rows.map((item) => item.clientId)
  single.value = rows.length != 1
  multiple.value = !rows.length
}
/** 新增按钮操作 */
const handleAdd = () => {
  setFormRef.value.open = true
  setFormRef.value.isAdd = true
  setFormRef.value.title = '添加终端'
}
/** 修改按钮操作 */
const handleUpdate = (row: { [key: string]: any }) => {
  setFormRef.value.isAdd = false
  setFormRef.value.open = true
  const clientId = row.clientId || ids
  setFormRef.value.getList(clientId)
}
/** 删除按钮操作 */
const handleDelete = (row: { [key: string]: any }) => {
  const rowList = JSON.parse(JSON.stringify(row))
  const id = rowList.clientId || ids

  ElMessageBox.confirm(
    `"是否确认删除用户编号为"${ id }"的数据项?"`,
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    },
  )
  .then(() => delClient(id))
  .then((res) => {
    if (res.code === 200) {
      console.log('ss')
      handleQuery()
      ElMessage.success('删除成功')
    } else {
      ElMessage.error(res.msg)
    }
  })
  .catch((error) => {
    if (error == 'cancel') {
      ElMessage.error(error)
    }
  })
}
const config = computed({
  get() {
    return {
      query: {
        fields: [
          {
            label: '终端编号',
            prop: 'clientId',
          },
        ],
        queryParams: queryParams.value,
      },
      btns: [
        {
          title: '新增',
          icon: Plus,
          directive: [['hasPermi', ['system:role:add']]],
          onClick: handleAdd,
        },
        {
          title: '修改',
          icon: Edit,
          directive: [['hasPermi', ['system:client:edit']]],
          onClick: handleUpdate,
        },
        {
          title: '删除',
          icon: Delete,
          directive: [['hasPermi', ['system:client:remove']]],
          onClick: handleDelete,
        },
      ],
      table: {
        columns: [
          { type: 'selection', width: '55', align: 'center' },
          {
            label: '编号',
            prop: 'clientId',
            align: 'center',
            width: '90',
          },
          {
            label: '安全码',
            prop: 'clientSecret',
            align: 'center',
            width: '90',
          },
          {
            label: '授权范围',
            prop: 'scope',
            align: 'center',
            width: '90',
          },
          {
            label: '授权类型',
            prop: 'authorizedGrantTypes',
            align: 'center',
            width: '90',
          },
          {
            label: '令牌时效',
            prop: 'accessTokenValidity',
            align: 'center',
            width: '90',
          },
          {
            label: '刷新时效',
            prop: 'refreshTokenValidity',
            align: 'center',
            width: '90',
          },
          {
            label: '操作',
            align: 'center',
            showOverflowTooltip: false,
            render: (h, params) => {
              const buttons = [
                {
                  title: '修改',
                  icon: Edit,
                  clickFun: handleUpdate,
                  directive: [['hasPermi', ['system:client:edit']]],
                },
                {
                  title: '删除',
                  icon: Delete,
                  clickFun: handleDelete,
                  directive: [['hasPermi', ['system:client:remove']]],
                },
              ]
              return buttons.map((el) => h(
                ElButton,
                {
                  text: true,
                  type: 'primary',
                  icon: el.icon,
                  onClick: () => {
                    el.clickFun(params.row)
                  },
                },
                {
                  default: () => el.title,
                },
              ))
            },
          },
        ],
        event: {
          onSelectionChange: (rows) => {
            handleSelectionChange(rows)
          },
        },
      },
      api: listClient,
    }
  },
  set(v) {
    return v
  },
})
</script>
